package com.masterclient.app.businessmodule.homemodule.view.adapter.feature;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.eoner.baselibrary.utils.ViewTransformUtil;
import com.eoner.managerlibrary.system.SystemConfigStorage;
import com.eoner.managerlibrary.user.UserDataManager;
import com.masterclient.app.R;
import com.masterclient.app.businessmodule.homemodule.bean.banner.SpecialImagBean;
import com.masterclient.app.businessmodule.homemodule.old.EventAdDataCallback;
import com.masterclient.app.businessmodule.homemodule.view.holder.HomeViewHolder;
import com.masterclient.app.businessmodule.usermodule.login.index.QuickLoginActivity;
import com.masterclient.app.modules.timelimited.TimeLimitedBuyActivity;
import com.masterclient.app.utils.CommonUtil;


public class HomePageTowPicAdapter extends DelegateAdapter.Adapter<HomeViewHolder> {

    private Context mContext;
    private LayoutHelper mLayoutHelper;
    private VirtualLayoutManager.LayoutParams mLayoutParams;
    private int mCount = 0;
    private SpecialImagBean featureBean;
    private EventAdDataCallback eventAdDataCallback;

    public void setEventAdDataCallback(EventAdDataCallback eventAdDataCallback) {
        this.eventAdDataCallback = eventAdDataCallback;
    }

    public HomePageTowPicAdapter(Context context, LayoutHelper layoutHelper, int count) {
        this(context, layoutHelper, count, null, null);
    }

    public HomePageTowPicAdapter(Context context, LayoutHelper layoutHelper, int count, @NonNull VirtualLayoutManager.LayoutParams layoutParams, SpecialImagBean featureBean) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.mCount = count;
        this.mLayoutParams = layoutParams;
        this.featureBean = featureBean;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @Override
    protected void onBindViewHolderWithOffset(HomeViewHolder holder, int position, int offsetTotal) {
    }

    @Override
    public HomeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return null;
    }

    @Override
    public void onBindViewHolder(HomeViewHolder holder, int position) {
        if (mLayoutParams != null) {
            holder.itemView.setLayoutParams(
                    new VirtualLayoutManager.LayoutParams(mLayoutParams));
        }

        View viewTowPic = holder.itemView.findViewById(R.id.view_tow_pic);
        ImageView ivLeft = holder.itemView.findViewById(R.id.iv_left);
        ImageView ivRight = holder.itemView.findViewById(R.id.iv_right);

        if (featureBean != null) {
            viewTowPic.setVisibility(View.VISIBLE);
            ViewTransformUtil.glideImageView(mContext, featureBean.getImg1(), ivLeft, new Transformation[]{new CenterCrop()}, R.mipmap.defaults_1);
            ViewTransformUtil.glideImageView(mContext, featureBean.getImg2(), ivRight, new Transformation[]{new CenterCrop()}, R.mipmap.defaults_1);
            ivLeft.setOnClickListener(v -> {
                Intent intent = new Intent(mContext, TimeLimitedBuyActivity.class);
                intent.putExtra("intentSource", "首页");
                mContext.startActivity(intent);
            });
            ivRight.setOnClickListener(v -> {
                if (UserDataManager.getInstance().isSystemLogin()) {
                    CommonUtil.goAnyWhere(mContext, SystemConfigStorage.getInstance().getMember_ship_url(), "", "", "", "");
                } else {
                    mContext.startActivity(new Intent(mContext, QuickLoginActivity.class));
                }
            });
        } else {
            viewTowPic.setVisibility(View.GONE);
        }

    }

    @Override
    public int getItemCount() {
        return mCount;
    }

    @Override
    public int getItemViewType(int position) {
        return 16;
    }
}
